package ai.people.platform.archives.service.impl;

import ai.people.netmon.framework.domain.gang.construction.bo.RelationLibConfigBo;
import ai.people.netmon.framework.domain.gang.construction.entity.RelationLibConfig;
import ai.people.netmon.framework.domain.gang.construction.entity.RelationRule;
import ai.people.netmon.framework.domain.gang.construction.request.RelationLibConfigListRequest;
import ai.people.netmon.framework.domain.gang.construction.request.RelationLibConfigSaveOrUpdateRequest;
import ai.people.netmon.framework.domain.gang.construction.request.RelationRuleRequest;
import ai.people.netmon.framework.domain.gang.construction.vo.RelationLibConfigVO;
import ai.people.netmon.framework.domain.gang.construction.vo.RelationRuleVO;
import ai.people.netmon.framework.exception.enums.CommonEnum;
import ai.people.netmon.framework.exception.enums.RelationLibExceptionEnum;
import ai.people.netmon.framework.exception.type.BusinessException;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.utils.AssertUtils;
import ai.people.platform.archives.service.RelationLibConfigService;
import ai.people.platform.archives.service.RelationLibService;
import ai.people.platform.archives.service.RelationRuleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhenglin
 * @since 2022-07-19
 */
@Service
public class RelationLibServiceImpl implements RelationLibService {
    @Autowired
    RelationRuleService relationRuleService;
    @Autowired
    RelationLibConfigService relationLibConfigService;


    /**
     * 关系规则列表
     *
     * @return {@link List}<{@link RelationRule}>
     */
    @Override
    public RelationRuleVO getRelationRule(RelationRuleRequest request) {
        LambdaQueryWrapper<RelationRule> queryWrapper = new LambdaQueryWrapper<RelationRule>()
                .eq(RelationRule::getExpListValue, request.getExpListValue()).eq(RelationRule::getExpTypeValue, request.getExpTypeValue());
        RelationRule one = relationRuleService.getOne(queryWrapper);
        if (one == null) {
            return null;
        }
        return new RelationRuleVO(one);
    }


    /**
     * 添加规则
     *
     * @param request 请求
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addConfig(RelationLibConfigSaveOrUpdateRequest request) {
        RelationLibConfig config = new RelationLibConfig(request);
        long cnt = relationLibConfigService.count(new LambdaQueryWrapper<RelationLibConfig>().eq(RelationLibConfig::getExpListValue, config.getExpListValue())
                .eq(RelationLibConfig::getExpTypeValue, config.getExpTypeValue()));
        if (cnt > 0) {
            throw new BusinessException(RelationLibExceptionEnum.EXIST);
        }
        boolean save = relationLibConfigService.save(config);
        AssertUtils.isTrue(save, CommonEnum.FAIL);
    }

    /**
     * 更新规则
     *
     * @param request 请求
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateConfig(RelationLibConfigSaveOrUpdateRequest request) {
        RelationLibConfig relationLibConfig = new RelationLibConfig(request);
        relationLibConfig.setId(request.getId());
        relationLibConfig.setUpdateTime(new Date());

        RelationLibConfig one = relationLibConfigService.getOne(new LambdaQueryWrapper<RelationLibConfig>().eq(RelationLibConfig::getExpListValue, request.getExpListValue())
                .eq(RelationLibConfig::getExpTypeValue, request.getExpTypeValue()));
        if (one != null) {
            if (!Objects.equals(one.getId(), request.getId())) {
                throw new BusinessException(RelationLibExceptionEnum.EXIST);
            }
        }
        boolean b = relationLibConfigService.updateById(relationLibConfig);
        AssertUtils.isTrue(b, CommonEnum.FAIL);
    }

    /**
     * 删除规则
     *
     * @param id id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteConfig(Long id) {
        boolean b = relationLibConfigService.removeById(id);
        AssertUtils.isTrue(b, CommonEnum.FAIL);
    }

    /**
     * 规则列表
     *
     * @param request 请求
     * @return {@link List}<{@link RelationLibConfigVO}>
     */
    @Override
    public PageResponse<RelationLibConfigVO> listConfig(RelationLibConfigListRequest request) {
        Page<RelationLibConfigBo> page = new Page<>(request.getOriginPageNo(), request.getPageSize());
        Page<RelationLibConfigBo> libConfigPage = relationLibConfigService.pageCustom(page);
        List<RelationLibConfigBo> records = libConfigPage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return PageResponse.builderEmpty();
        }
        List<RelationLibConfigVO> voList = new ArrayList<>();
        for (RelationLibConfigBo record : records) {
            voList.add(new RelationLibConfigVO(record));
        }
        return PageResponse.builder(voList, libConfigPage.getTotal(), libConfigPage.getPages(), libConfigPage.getCurrent());
    }

}
